package com.yeqian.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @Author:yeqian
 * @Date: 2020/12/2 13:30
 */

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    /**配置了swagger的docket实例,接管原来的默认配置
     * 浏览器中输入http://localhost/swagger-ui/index.html 进入swagger的ui界面
     * @param environment
     * @return
     */
    @Bean
    public Docket getDocket(Environment environment) {

        //设置显示的swagger环境
        Profiles profiles = Profiles.of("development");
        //判断是否处于当前环境
        boolean flag = environment.acceptsProfiles(profiles);
        System.out.println("是否处于本地环境：" + flag);
        //通过environment.acceptsProfiles判断当前处于什么环境
        return new Docket(DocumentationType.SWAGGER_2).groupName("main")
                .apiInfo(apiInfo())
                .enable(flag) //如果enable为false代表swagger不能在浏览器中访问
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yeqian.controller")) //指定要扫描的包
                .build();
    }



    //配置swagger信息===ApiInfo
    private ApiInfo apiInfo() {
        Contact contact = new Contact("叶纤", "https://rrps.world.top/", "798737426@qq.com");
        return new ApiInfo(
                "叶纤的SwaggerApi文档",
                "尽人事，听天命",
                "v1.0",
                "https://rrps.world.top/",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
